package com.efast.dmom.mes.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.OperationWorkorder;
import com.efast.dmom.mes.bean.Plan;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.OperationWorkorderDao;
import com.efast.dmom.mes.dao.entity.PlanDao;
import com.efast.dmom.mes.datahandlers.base.DataHandler;
import com.efast.dmom.mes.entity.OperationWorkorderEntity;
import com.efast.dmom.mes.entity.PlanEntity;
import com.efast.dmom.mes.util.CommonConstants;
import com.efast.dmom.mes.util.RuleCodeGeneratorUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

@Component
public class PlanDataHandler extends DataHandler<Plan>{
	
	@Autowired
	private PlanDao planDao;
	
	@Autowired
	private RuleCodeGeneratorUtil ruleCodeGeneratorUtil;
	
	@Autowired
	private OperationWorkorderDao operationWorkorderDao;
	
	@Override
	public ResultBean save(Plan obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		PlanEntity planEntity = obj.getInnerData();
		planEntity.setPlanNo(ruleCodeGeneratorUtil.getNo(CommonConstants.PLAN_CODE));
		planEntity.setPlanStatus(CommonConstants.JRD_PLAN_STATUS_CREATED);
		planEntity.setCreationTime(TimeUtil.getLocalDateTime());
		planEntity.setCreator(SpringWebUtils.getRequestUser());
		planEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		planEntity.setLastModifier(SpringWebUtils.getRequestUser());
		planEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		planEntity.setRowStatus(1);
		planDao.save(planEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}

	@Override
	public ResultBean update(Plan obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		PlanEntity planEntity = obj.getInnerData();
		planEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		planEntity.setLastModifier(SpringWebUtils.getRequestUser());
		planDao.update(planEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("更新成功！");
		return resultBean;
	}

	@Override
	public ResultBean delete(Plan obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		PlanEntity planEntity = obj.getInnerData();
		planEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		planEntity.setLastModifier(SpringWebUtils.getRequestUser());
		planDao.delete(planEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	@Override
	public ResultBean saveBatch(List<Plan> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Plan> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Plan> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Plan getById(long id) {
		PlanEntity planEntity = planDao.getById(id);
		Plan plan = new Plan();
		if (planEntity != null) {
			plan.setInnerData(planEntity);
		}
		return plan;
	}

	public ResultBean closePlan(Plan plan) {
		ResultBean resultBean = new ResultBean(true,' ');
		PlanEntity planEntity = plan.getInnerData();
		Condition condition = new Condition(OperationWorkorderEntity.class);
		Criteria createCriteria = condition.createCriteria();
		createCriteria.andEqualTo("planNo", planEntity.getPlanNo());
		List<OperationWorkorderEntity> selectByExample = operationWorkorderDao.selectByExample(condition);
		planEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		planEntity.setLastModifier(SpringWebUtils.getRequestUser());
		planEntity.setPlanStatus(CommonConstants.JRD_PLAN_STATUS_CLOSED);
		planDao.closePlan(planEntity);
		if (selectByExample != null && selectByExample.size() > 0) {
		 for (OperationWorkorderEntity entity: selectByExample) {
			 OperationWorkorder operationWorkorder2 = new OperationWorkorder();
				operationWorkorder2.setId(entity.getId());
				operationWorkorder2.setLastUpdateTime(TimeUtil.getLocalDateTime());
				operationWorkorder2.setLastUpdateUser(SpringWebUtils.getRequestUser());
				operationWorkorder2.setOrderStatus(2L);
				operationWorkorderDao.updateByPrimaryKeySelective(operationWorkorder2);
		    }	
		}
		resultBean.setSuccess(true);
		resultBean.setMessage("关闭成功！");
		return resultBean;
	}

}
